package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

/**
 * 描述：飞机游戏的主窗口
 *
 * @author wangb
 * @version 1.0
 * create 2018-11-01-22:10
 */
public class MyGameFrame extends JFrame{
    Image planeImg = GameUtil.getImage ("images/plane.png");
    Image bg = GameUtil.getImage ("images/bg.jpg");
    Plane plane = new Plane (planeImg,250,250);
    //Shell shell = new Shell ();//生成一个炮弹
    Shell[] shells = new Shell[20];//生成50个炮弹
    Explode explode;//生明一个爆炸对象
    private Image offScreenImage = null;
    Date startTime = new Date ();//开始的这个时间对象就直接创建
    Date endTime ;//死亡的这个对象在游戏结束的时候再创建。
    int period;//游戏持续的时间
    /**
     * 帮助我们画窗口，自动调用
     * g相当于一个画笔
     */
    @Override
    public void paint(Graphics g) {
        Color c = g.getColor ();
        g.drawImage (bg,0,0,null);//画背景
        plane.drawSelf (g);//画飞机
        //shell.draw (g);//画一个炮弹出来

        //画一堆炮弹
        for ( int i = 0 ; i < shells.length; i++ )
        {
            shells[i].draw (g);
            boolean peng = shells[i].getRect ().intersects (plane.getRect ());//判断两个矩形有咩有相交

            if(peng){
                plane.isLive = false;//撞上了之后飞机就死了。

                //对象只创建一次。
                if(explode == null){
                    explode = new Explode (plane.x,plane.y);
                    endTime = new Date ();
                    period = (int)(endTime.getTime () - startTime.getTime ())/1000;//游戏持续的时间
                }

                explode.draw (g);

                if(!plane.isLive){
                    //如果飞机死了，就一直显示这个数字
                    Font font = new Font ("宋体",Font.BOLD,50);
                    g.setFont (font);
                    g.setColor (Color.RED);
                    g.drawString ("时间："+period+"秒",(int)plane.x,(int)plane.y);

                }            }
        }
        g.setColor (c);
    }


    //帮助我们反复重画窗口
    class PaintThread extends Thread{
        //内部类，好处是可以使用外部类的方法

        @Override
        public void run( ) {
            while(true){

                repaint ();//表示重画窗口，paint是系统调用的，用户不可以调用
                try {
                    Thread.sleep (40);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        }
    }


    /**
     * 键盘监听，内部类
     */
    class KeyMonitor extends KeyAdapter{

        /**
         * 按下按键
         * @param e
         */
        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println ("按下："+e.getKeyCode ());
            plane.addDirection (e);
        }

        /**
         * 抬起了哪个按键
         * @param e
         */
        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println ("松开:"+e.getKeyCode ());
            plane.minusDirection (e);
        }
    }


    /**
     * 初始化窗口
     */
    public void launchFrame(){
        setTitle ("飞机大战BY Wangb");
        setVisible (true);//设置窗口可见
        setSize (Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//设置窗口大小
        setLocation (300,300);//设置位置

        /**
         * 初始化的时候生成50个炮弹
         */
        for ( int i = 0 ; i < shells.length; i++ )
        {
            shells[i] = new Shell ();
        }


        /**
         * 匿名内部类
         */
        this.addWindowListener (new WindowAdapter (){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit (0);
            }

        });

        new PaintThread ().start ();//启动重画窗口的线程
        addKeyListener (new KeyMonitor ());//给窗口增加键盘监听
    }

    /**
     * 双缓存解决闪烁问题
     * @param g 画笔
     */
    public void update(Graphics g){
        if(offScreenImage == null)
            offScreenImage = this.createImage (Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
        Graphics gOff = offScreenImage.getGraphics ();
        paint (gOff);
        g.drawImage (offScreenImage,0,0,null);

    }


    public static void main(String[] args){
        MyGameFrame myGameFrame = new MyGameFrame ();
        myGameFrame.launchFrame ();
    }
}
